//rpc go 框架 客户端测试
package main

import (
	serviceproxy "example/service/proxy"
	dynamicproxy "example/servicedynamic/proxy"
	"flag"
	"fmt"
	"gitee.com/dennis-kk/rpc-go-backend/idlrpc"
	idlnet "gitee.com/dennis-kk/rpc-go-backend/idlrpc/net"
	"log"
	"net/http"
	_ "net/http/pprof"
	"os"
	"rpc-repo/gomock/utiltool"
	"strings"
	"time"
)

var ()

var (
	port    string
	addr    string
	profile string
)

func printHelp() {
	bin := os.Args[0]
	if i := strings.LastIndex(bin, "/"); i != -1 {
		bin = bin[i+1:]
	}

	fmt.Printf("Usage %s -I example.idl -O impl/goservice", bin)
	flag.PrintDefaults()
}

func main() {
	flag.Usage = printHelp //解析出错时候 调用的默认方法
	flag.StringVar(&port, "p", "", "listen port")
	flag.StringVar(&addr, "h", "", "address")
	flag.StringVar(&profile, "f", "", "cpurprofile")
	flag.Parse()

	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
	//初始化Caller
	//初始化 client 这里不用注册，package 里面自带了注册代码你自己管理自己获取就好了
	caller := &MockCaller{}

	//初始化connect
	caller.Connector = utiltool.NewMockConnector()
	if caller.Connector == nil {
		fmt.Printf("[Proxy] Create Connector Error !!!")
		return
	}

	if err := idlnet.ConnectTo("tcp", addr+":"+port, caller.Connector); err != nil {
		panic(err)
	}

	caller.Serviceclient = serviceproxy.NewServiceProxy(utiltool.GetRandomTrans())
	caller.Dynamicclient = dynamicproxy.NewServiceDynamicProxy(utiltool.GetRandomTrans())

	idlnet.Start()
	idlrpc.Start()

	caller.Dura = time.Now()

	for {
		caller.DoTest()
	}

}
